package com.aiit.controller;

import com.aiit.pojo.bo.Task;
import com.aiit.pojo.bo.User;
import com.aiit.service.HomeService;
import com.aiit.util.DateFormatUtil;
import com.aiit.util.GetJsonUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@CrossOrigin
@RequestMapping("home")
public class HomeController {
    @Autowired
    private HomeService homeService;

    /**
     * 根据用户的已办状态查询事务记录
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tasksTrueStatus",produces ={"application/json;charset=UTF-8"})
    public String selectTrueStatus(HttpServletRequest request){
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        List<Task> list=homeService.selectTrueStatus(user.getId());
        for (Task task : list) {
            task.setCreateTime(DateFormatUtil.DateFormat(task.getCreate_time()));
            task.setFinishTime(DateFormatUtil.DateFormat(task.getFinish_time()));
        }
        return GetJsonUtil.getTaskString(list);
    }

    /**
     * 根据用户的未办状态查询事务记录
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tasksFalseStatus",produces ={"application/json;charset=UTF-8"})
    public String selectFalseStatus(HttpServletRequest request){
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        List<Task> list=homeService.selectFalseStatus(user.getId());
        for (Task task : list) {
            task.setCreateTime(DateFormatUtil.DateFormat(task.getCreate_time()));
            task.setFinishTime(DateFormatUtil.DateFormat(task.getFinish_time()));
        }
        return GetJsonUtil.getTaskString(list);
    }

    /**
     * 首页已办(时间是以24小时算一天，不是以多少号分界的,百分比以天做单位)
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "searchTaskByHomeFinish",produces = {"application/json;charset=UTF-8"})
    public @ResponseBody String searchTaskByHomeFinish(HttpServletRequest request) throws Exception {
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");

        List<Task> tasks=homeService.searchTaskByHomeFinish(user.getId());

        Date begin;
        Date end;
        Date present;
        List<Map<String,Object>> r=new ArrayList<Map<String, Object>>();

        for (Task task : tasks) {
            Map<String,Object> maps=new HashMap<String, Object>();
            //算时间
            begin=task.getCreate_time();
            end=task.getFinish_time();
            present=new Date();
            int all=(int)((float) (end.getTime()-begin.getTime())/86400000);//总天数
            int pass=(int)((float) (present.getTime()-begin.getTime())/86400000);//过去天数
            int percent=0;//百分比
            if(all==0){
                percent=100;
            }else {
                percent=(int)(((float)pass/all)*100);//百分比
            }
            int remain=(all-pass)>=0?all-pass:0;//剩余天数
            //存数据
            maps.put("id",task.getId());
            maps.put("title",task.getTitle());
            maps.put("type",task.getType());
            maps.put("task",task.getTask());
            maps.put("time",remain);
            maps.put("plan",percent);
            r.add(maps);
        }
        JSONObject res=new JSONObject();
        res.put("code",0);
        res.put("msg","success");
        res.put("data",r);
        return res.toJSONString();
    }
    /**
     * 首页未办
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "searchTaskByHomeUNFinish",produces = {"application/json;charset=UTF-8"})
    public @ResponseBody String searchTaskByHomeUNFinish(HttpServletRequest request) throws Exception {
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");

        List<Task> tasks=homeService.searchTaskByHomeUNFinish(user.getId());
        System.out.println("userid："+user.getId());
        System.out.println(tasks);


        Date begin;
        Date end;
        Date present;
        List<Map<String,Object>> r=new ArrayList<Map<String, Object>>();

        for (Task task : tasks) {
            Map<String,Object> maps=new HashMap<String, Object>();
            //算时间
            begin=task.getCreate_time();
            end=task.getFinish_time();
            present=new Date();
            int all=(int)((float) (end.getTime()-begin.getTime())/86400000);//总天数
            int pass=(int)((float) (present.getTime()-begin.getTime())/86400000);//过去天数
            int percent=0;//百分比
            if(all==0){
                percent=100;
            }else {
                percent=(int)(((float)pass/all)*100);//百分比
            }
            int remain=(all-pass)>=0?all-pass:0;//剩余天数
            //System.out.println(task.getId());
            //存数据
            maps.put("id",task.getId());
            maps.put("title",task.getTitle());
            maps.put("type",task.getType());
            maps.put("task",task.getTask());
            maps.put("time",remain);
            maps.put("plan",percent);
            r.add(maps);
        }
        JSONObject res=new JSONObject();
        res.put("code",0);
        res.put("msg","success");
        res.put("data",r);
        System.out.println("当前数据"+res);
        return res.toJSONString();
    }

    /**
     *更多中根据已办状态和用户查询所有的事务记录
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "searchTrueTaskByStatusandUser",produces = {"application/json;charset=UTF-8"})
    public @ResponseBody String searchTrueTaskByStatusandUser(HttpServletRequest request) throws Exception {
        String title = request.getParameter("title");
        String create_time = request.getParameter("start");
        String finish_time = request.getParameter("end");
        System.out.println(title);
        System.out.println(create_time);
        System.out.println(finish_time);

        User user= (User) request.getSession().getAttribute("user");
        System.out.println("成功");
        //成功
        List<Task> tasks = homeService.searchTrueTaskByStatusandUser(title,create_time,finish_time,user.getId());
        for (Task task : tasks) {
            task.setCreateTime(DateFormatUtil.DateFormat(task.getCreate_time()));
            task.setFinishTime(DateFormatUtil.DateFormat(task.getFinish_time()));
        }
        System.out.println(GetJsonUtil.getTaskString(tasks));
        return GetJsonUtil.getTaskString(tasks);
    }

    /**
     * 更多中根据待办状态和用户查询所有的事务记录
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "searchFalseTaskByStatusandUser",produces = {"application/json;charset=UTF-8"})
    public @ResponseBody String searchFalseTaskByStatusandUser(HttpServletRequest request) throws Exception {
        String title = request.getParameter("title");
        String create_time = request.getParameter("start");
        String finish_time = request.getParameter("end");
        User user= (User) request.getSession().getAttribute("user");
        System.out.println("成功");
        //成功
        List<Task> tasks = homeService.searchFalseTaskByStatusandUser(title,create_time,finish_time, user.getId());
        for (Task task : tasks) {
            task.setCreateTime(DateFormatUtil.DateFormat(task.getCreate_time()));
            task.setFinishTime(DateFormatUtil.DateFormat(task.getFinish_time()));
        }
        return GetJsonUtil.getTaskString(tasks);
    }

    /**
     * 事务task的状态切换 已办-》待办
     * @param id
     */
    @RequestMapping(value = "updateTaskByStatus1",produces = {"application/json;charset=UTF-8"})
    public @ResponseBody String updateTaskByStatus1(@RequestParam String id){
        homeService.updateTaskByStatus1(Integer.parseInt(id));
        return "0";

    }

    /**
     * 事务task的状态切换 待办-》已办
     * @param id
     */
    @RequestMapping(value = "updateTaskByStatus2",produces = {"application/json;charset=UTF-8"})
    public @ResponseBody String updateTaskByStatus2(@RequestParam String id){
        homeService.updateTaskByStatus2(Integer.parseInt(id));
        return "0";
    }

    /**
     * 修改密码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "modifyPassword",produces = {"application/json;charset=UTF-8"})
    public void modifyPassword(HttpServletRequest request){
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        Long userid=user.getId();
//        Long userid=10001l;

        String newpwd=request.getParameter("newpwd");
        int res=homeService.modifyPassword(newpwd,userid);
        System.out.println(res);

    }


}
